import pygame
import math

# 初始化 Pygame
pygame.init()

# 设置窗口大小
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("月球降落游戏")

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 游戏参数
GRAVITY = 0.1  # 月球重力加速度（简化）
THRUST = 0.2   # 推进力
ROTATION_SPEED = 3  # 旋转速度（度/帧）
FUEL_CONSUMPTION = 1  # 每帧燃料消耗

# 着陆器类
class Lander:
    def __init__(self):
        self.x = WIDTH // 2
        self.y = 100
        self.vx = 0
        self.vy = 0
        self.angle = 0  # 角度，0 为向上
        self.fuel = 500
        self.landed = False
        self.crashed = False

    def update(self, thrusting):
        # 应用重力
        self.vy += GRAVITY

        # 如果推进且有燃料
        if thrusting and self.fuel > 0:
            rad = math.radians(self.angle)
            self.vx += math.sin(rad) * THRUST
            self.vy -= math.cos(rad) * THRUST  # 向上推进减少 vy
            self.fuel -= FUEL_CONSUMPTION
            if self.fuel < 0:
                self.fuel = 0

        # 更新位置
        self.x += self.vx
        self.y += self.vy

        # 边界检查（简单，防止出屏）
        if self.x < 0:
            self.x = 0
            self.vx = -self.vx * 0.5
        elif self.x > WIDTH:
            self.x = WIDTH
            self.vx = -self.vx * 0.5

    def rotate_left(self):
        self.angle += ROTATION_SPEED

    def rotate_right(self):
        self.angle -= ROTATION_SPEED

    def draw(self, screen):
        # 绘制着陆器（简单三角形）
        points = [
            (self.x + 15 * math.sin(math.radians(self.angle)), self.y - 15 * math.cos(math.radians(self.angle))),
            (self.x + 10 * math.sin(math.radians(self.angle + 120)), self.y - 10 * math.cos(math.radians(self.angle + 120))),
            (self.x + 10 * math.sin(math.radians(self.angle - 120)), self.y - 10 * math.cos(math.radians(self.angle - 120))),
        ]
        pygame.draw.polygon(screen, WHITE, points)

        # 如果推进，绘制火焰
        if self.fuel > 0:
            flame_points = [
                (self.x + 10 * math.sin(math.radians(self.angle + 180)), self.y - 10 * math.cos(math.radians(self.angle + 180))),
                (self.x + 5 * math.sin(math.radians(self.angle + 150)), self.y - 5 * math.cos(math.radians(self.angle + 150))),
                (self.x + 5 * math.sin(math.radians(self.angle - 150)), self.y - 5 * math.cos(math.radians(self.angle - 150))),
            ]
            pygame.draw.polygon(screen, RED, flame_points)

# 月球表面（简单直线，实际可做地形）
GROUND_Y = HEIGHT - 50

def draw_ground(screen):
    pygame.draw.line(screen, GREEN, (0, GROUND_Y), (WIDTH, GROUND_Y), 5)
    # 着陆平台（简单矩形）
    pygame.draw.rect(screen, BLUE, (WIDTH//2 - 50, GROUND_Y - 10, 100, 10))

# 主游戏循环
def main():
    clock = pygame.time.Clock()
    lander = Lander()
    running = True
    thrusting = False

    while running:
        screen.fill(BLACK)
        draw_ground(screen)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    thrusting = True
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    thrusting = False

        # 按键处理
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            lander.rotate_left()
        if keys[pygame.K_RIGHT]:
            lander.rotate_right()

        lander.update(thrusting)
        lander.draw(screen)

        # 显示燃料
        font = pygame.font.SysFont(None, 30)
        fuel_text = font.render(f"Fuel: {int(lander.fuel)}", True, WHITE)
        screen.blit(fuel_text, (10, 10))

        # 速度显示
        speed = math.sqrt(lander.vx**2 + lander.vy**2)
        speed_text = font.render(f"Speed: {speed:.2f}", True, WHITE)
        screen.blit(speed_text, (10, 40))

        # 检查着陆
        if lander.y >= GROUND_Y - 15:  # 着陆器底部接触地面
            if abs(lander.vy) > 3 or abs(lander.vx) > 2 or abs(lander.angle) > 10:
                lander.crashed = True
                crash_text = font.render("Crashed!", True, RED)
                screen.blit(crash_text, (WIDTH//2 - 50, HEIGHT//2))
            else:
                lander.landed = True
                land_text = font.render("Landed Successfully!", True, GREEN)
                screen.blit(land_text, (WIDTH//2 - 100, HEIGHT//2))
            lander.vy = 0
            lander.vx = 0
            lander.y = GROUND_Y - 15

        # 如果着陆或坠毁，停止推进
        if lander.landed or lander.crashed:
            thrusting = False
            # 可以添加重玩逻辑，但这里简单停止

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    main()